/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PlaylistCellWithArtPanel.java
 *
 * Created on Mar 8, 2009, 2:36:51 PM
 */
package com.bff.slimserver.musicjukebox.ui.components;

import com.bff.slimserver.music.exceptions.PlaylistException;
import com.bff.bjj.ui.component.ColorProperties;
import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import com.bff.slimserver.musicjukebox.lib.actions.AddFavoriteAction;
import com.bff.slimserver.musicjukebox.lib.actions.PlaylistDownAction;
import com.bff.slimserver.musicjukebox.lib.actions.PlaylistUpAction;
import com.bff.slimserver.musicjukebox.ui.ThumbnailPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JList;
import org.bff.slimserver.SlimPlaylist;
import org.bff.slimserver.musicobjects.SlimPlaylistItem;
import org.bff.slimserver.musicobjects.SlimSong;

/**
 *
 * @author Music
 */
public class PlaylistCellWithArtPanel extends javax.swing.JPanel implements PlaylistCellPanelIface {

    private boolean selected;
    private JButton btnPlaylistUp;
    private JButton btnPlaylistDown;
    private JButton btnFavorite;
    private Action playlistUpAction;
    private Action playlistDownAction;
    private Action addFavoriteAction;
    private SlimPlaylist playlist;
    private JList listPlaylist;
    private SlimSong song;
    private ThumbnailPanel tp;

    /**
     * Creates new form PlaylistCellWithArtPanel
     * Delay is the number of milliseconds to wait before loading the thumbnail
     */
    public PlaylistCellWithArtPanel(final SlimSong song, JList listPlaylist, final int delay) {

        initComponents();
        this.song = song;
        this.listPlaylist = listPlaylist;
        try {
            this.playlist = JukeboxProperties.getInstance().getPlaylist();
        } catch (PlaylistException ex) {
            Logger.getLogger(PlaylistCellWithArtPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

        tp = new ThumbnailPanel();
        tp.setPreferredSize(new Dimension(100, 100));
        artPanel.add(tp, BorderLayout.CENTER);

        repaint();


        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(PlaylistCellWithArtPanel.class.getName()).log(Level.SEVERE, null, ex);
                }

                try {
                    Thread.sleep(delay);
                } catch (InterruptedException ex) {
                    Logger.getLogger(PlaylistCellWithArtPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
                tp.loadImage(song, 0);

            }
        }).start();

        lblTitle.setText(song.getTitle());
        lblAlbum.setText(song.getAlbum().getName());
        lblArtist.setText(song.getArtist());

        playlistUpAction = new PlaylistUpAction(listPlaylist, getPlaylist());
        playlistDownAction = new PlaylistDownAction(listPlaylist, getPlaylist());
        addFavoriteAction = new AddFavoriteAction(song);

        btnPlaylistUp = new javax.swing.JButton();
        btnPlaylistUp.setContentAreaFilled(false);
        btnFavorite = new javax.swing.JButton();
        btnFavorite.setContentAreaFilled(false);
        btnPlaylistDown = new javax.swing.JButton();
        btnPlaylistDown.setContentAreaFilled(false);


        btnPlaylistUp.setBackground(java.awt.Color.lightGray);
        btnPlaylistUp.setBorder(null);
        btnPlaylistUp.setBorderPainted(false);
        btnPlaylistUp.setFocusPainted(false);
        btnPlaylistUp.setIconTextGap(0);
        btnPlaylistUp.setMargin(new java.awt.Insets(0, 0, 0, 0));
        btnPlaylistUp.setMaximumSize(new java.awt.Dimension(50, 50));
        btnPlaylistUp.setMinimumSize(new java.awt.Dimension(16, 16));
        btnPlaylistUp.setPreferredSize(new java.awt.Dimension(16, 16));

        btnPlaylistDown.setBorder(null);
        btnPlaylistDown.setBorderPainted(false);
        btnPlaylistDown.setIconTextGap(0);
        btnPlaylistDown.setMargin(new java.awt.Insets(0, 0, 0, 0));
        btnPlaylistDown.setMaximumSize(new java.awt.Dimension(50, 50));
        btnPlaylistDown.setMinimumSize(new java.awt.Dimension(16, 16));
        btnPlaylistDown.setPreferredSize(new java.awt.Dimension(16, 16));

        btnFavorite.setBorder(null);
        btnFavorite.setBorder(null);
        btnFavorite.setIconTextGap(0);
        btnFavorite.setMargin(new java.awt.Insets(0, 0, 0, 0));
        btnFavorite.setMaximumSize(new java.awt.Dimension(50, 50));
        btnFavorite.setMinimumSize(new java.awt.Dimension(16, 16));
        btnFavorite.setPreferredSize(new java.awt.Dimension(16, 16));

        btnPlaylistUp.setAction(playlistUpAction);
        btnPlaylistDown.setAction(playlistDownAction);
        btnFavorite.setAction(addFavoriteAction);
        btnPlaylistDown.setText(null);
        btnPlaylistUp.setText(null);
        btnFavorite.setText(null);

        btnPanel.setBackground(ColorProperties.LIST_BACKGROUND_SELECTED);
        btnPanel.add(btnFavorite);
        btnPanel.add(btnPlaylistUp);
        btnPanel.add(btnPlaylistDown);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        artPanel = new javax.swing.JPanel();
        detailPanel = new javax.swing.JPanel();
        songPanel = new javax.swing.JPanel();
        lblTitle = new javax.swing.JLabel();
        lblAlbum = new javax.swing.JLabel();
        lblArtist = new javax.swing.JLabel();
        btnPanel = new javax.swing.JPanel();

        setLayout(new java.awt.BorderLayout());

        artPanel.setBackground(new java.awt.Color(0, 0, 0));
        artPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        artPanel.setPreferredSize(new java.awt.Dimension(100, 100));
        artPanel.setLayout(new java.awt.BorderLayout());
        add(artPanel, java.awt.BorderLayout.WEST);

        detailPanel.setLayout(new java.awt.BorderLayout());

        songPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
        songPanel.setLayout(new java.awt.GridBagLayout());

        lblTitle.setText("jLabel1");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        songPanel.add(lblTitle, gridBagConstraints);

        lblAlbum.setText("jLabel2");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        songPanel.add(lblAlbum, gridBagConstraints);

        lblArtist.setText("jLabel3");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        songPanel.add(lblArtist, gridBagConstraints);

        detailPanel.add(songPanel, java.awt.BorderLayout.WEST);
        detailPanel.add(btnPanel, java.awt.BorderLayout.EAST);

        add(detailPanel, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel artPanel;
    private javax.swing.JPanel btnPanel;
    private javax.swing.JPanel detailPanel;
    private javax.swing.JLabel lblAlbum;
    private javax.swing.JLabel lblArtist;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JPanel songPanel;
    // End of variables declaration//GEN-END:variables

    public void setEven() {
        setBackground(ColorProperties.LIST_BACKGROUND_EVEN);
    }

    public void setOdd() {
        setBackground(ColorProperties.LIST_BACKGROUND_ODD);
    }

    public void setCurrentPlaying() {
        setForeground(ColorProperties.PLAYLIST_CURRENT_PLAYING_FOREGROUND);
    }

    public void setSelected() {
        setForeground(ColorProperties.LIST_FOREGROUND_SELECTED);
        setBackground(ColorProperties.LIST_BACKGROUND_SELECTED);
        setButtonsVisible(true);
    }

    public void setNotSelected() {
        setForeground(ColorProperties.LIST_FOREGROUND);
        setButtonsVisible(false);
    }

    @Override
    public void setBackground(Color bg) {
        super.setBackground(bg);
        //detailPanel.setBackground(bg);
        if (detailPanel != null) {
            detailPanel.setBackground(bg);
            songPanel.setBackground(bg);
        }
    }

    @Override
    public void setForeground(Color fg) {
        super.setForeground(fg);
        if (detailPanel != null) {
            detailPanel.setForeground(fg);
            songPanel.setForeground(fg);
        }
        setLabelColors(fg);
    }

    private void setLabelColors(Color color) {
        if (lblTitle != null) {
            lblTitle.setForeground(color);
            lblArtist.setForeground(color);
            lblAlbum.setForeground(color);
        }
    }

    @Override
    public Component getPanel() {
        return this;
    }

    /**
     * @return the selected
     */
    public boolean isSelected() {
        return selected;
    }

    public SlimPlaylist getPlaylist() {
        return playlist;
    }

    public void setPlaylist(SlimPlaylist playlist) {
        this.playlist = playlist;
    }

    public void setUpAction(boolean enabled) {
        playlistUpAction.setEnabled(enabled);
    }

    public void setDownAction(boolean enabled) {
        playlistDownAction.setEnabled(enabled);
    }

    public void eventReceived(java.awt.event.MouseEvent evt) {

        int index = listPlaylist.locationToIndex(evt.getPoint());

        if (index < 1) {
            btnPlaylistUp.setEnabled(false);
        } else {
            btnPlaylistUp.setEnabled(true);
        }

        if (index >= listPlaylist.getModel().getSize() - 1) {
            btnPlaylistDown.setEnabled(false);
        } else {
            btnPlaylistDown.setEnabled(true);
        }

        listPlaylist.repaint();

    }

    private void setButtonsVisible(boolean visible) {
        btnPanel.setVisible(visible);
    }

    public void setCurrent() {
        setLabelColors(ColorProperties.PLAYLIST_CURRENT_PLAYING_FOREGROUND);
    }

    public void setNotCurrent() {
        setLabelColors(ColorProperties.LIST_FOREGROUND_SELECTED);
    }

    @Override
    public void loadThumbnail() {
        tp.loadImage(song);
    }

    @Override
    public void setNotPlaying() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public SlimPlaylistItem getItem() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
